package com.cxria.problem.entity


import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails

@JsonIgnoreProperties(['password', 'authorities', 'accountNonExpired', 'accountNonLocked', 'credentialsNonExpired', 'enabled'])
class User implements UserDetails, Serializable {

	Integer userId

	String username

	String password

	List<Role> roles

	@Override
	List<GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> auths = new ArrayList<>()
		if(roles != null){
			for(Role role : roles){
				for(Permission authority : role.getPermissions()){
					auths.add(new SimpleGrantedAuthority(authority.getPermissionName()))
				}
			}
		}
		return auths
	}

	@Override
	boolean isAccountNonExpired() {
		return true
	}

	@Override
	boolean isAccountNonLocked() {
		return true
	}

	@Override
	boolean isCredentialsNonExpired() {
		return true
	}

	@Override
	boolean isEnabled() {
		return true
	}

}